//
//  TKIMRecordView.m
//  TKIM
//
//  Created by liupm on 15/10/26.
//  Copyright © 2015年 liupm. All rights reserved.
//

#import "TKIMRecordView.h"
#import "TKIMLayerUtils.h"


@interface TKIMRecordView()<TKIMAudioDelegate>
@property(nonatomic,strong) TKIMAudioHelper *audioHelper;
@property(nonatomic,strong) TKIMAudioHelper *playHelper;
//是否录制成功
@property(nonatomic,assign) BOOL isRecordSuc;
//手指的位置
@property(nonatomic,assign) CGPoint fingerPoint;
//音频波动容器
@property (strong, nonatomic)  UIView *audioPowerView;
//音频波动
@property (strong, nonatomic)  UIImageView *audioPowerImageView;
//音频标签
@property (strong, nonatomic)  UILabel *audioPowerLabel;
@end

static float recordMinTime = 1.0f;

@implementation TKIMRecordView
{
    NSMutableArray *_dataArray;
}

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.row = 1;
        self.cloum =1;
        self.isShowPageCount = NO;
        _isRecordSuc = YES;
        [self initData];
    }
    return self;
}


/**
 *  @Author lpm, 15-08-03 14:08:18
 *
 *  @brief 初始化表情数据
 */
-(void)initData
{
    _audioHelper = [[TKIMAudioHelper alloc] init];
    _playHelper = [[TKIMAudioHelper alloc] init];

    _audioHelper.delegate = self;
    _dataArray = [self getConfigDataByKey:@"voice"];
    [self drawView:_dataArray];
}


/**
 *  @Author lpm, 15-08-03 16:08:19
 *
 *  @brief  获得子元素
 *
 */
-(UIView *)getItem:(NSMutableDictionary *)data frame:(CGRect)rect
{
    UIView *view = [[UIView alloc] initWithFrame:rect];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, rect.size.width, rect.size.height*0.25)];
    label.font = [UIFont systemFontOfSize:16.0f];
    label.textColor = [UIColor grayColor];
    label.textAlignment = NSTextAlignmentCenter;
    label.backgroundColor = [UIColor clearColor];
    label.text = data[@"key"];

    
    NSString *imgUrl = data[@"value"];
    UIImage *image = TKIMGetImage(imgUrl);
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake((rect.size.width-rect.size.height*0.6)/2, label.frame.size.height+label.frame.origin.y+5, rect.size.height*0.6, rect.size.height*0.6)];
    [button setImage:image forState:UIControlStateNormal];
    button.backgroundColor = [UIColor clearColor];
    
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
    longPress.minimumPressDuration = 0.0;
    [button addGestureRecognizer:longPress];
    
    [view addSubview:label];
    [view addSubview:button];
    return view;
}


/**
 *  @Author lpm, 15-10-26 13:10:01
 *
 *  @brief  长按说话
 *
 *  @param press
 */
-(void)longPress:(UIGestureRecognizer *)press
{
    CGPoint point =  [press locationInView:self];
    switch (press.state)
    {
        case UIGestureRecognizerStateBegan:
        {
            NSLog(@"开始录制");
            [self routerEventWithName:TKAudioRecordEvent userInfo:nil];
            _audioPowerLabel.text = @"手指上滑,取消发送";
            if ([_audioHelper canRecord])
            {
                [self showPowerView:YES];
            }
            [_audioHelper record];

        }
            break;
        case UIGestureRecognizerStateEnded:
            if ([_audioHelper canRecord])
            {
                _fingerPoint = point;
                [self setVoiceImage:0.0];
                [self showPowerView:NO];
                //停止录制
                [_audioHelper stopRecord];
            }
            break;
        case UIGestureRecognizerStateChanged:
            if(point.y < -50)
            {
                _audioPowerLabel.text = @"松开手指,取消发送";
            }
            else
            {
                _audioPowerLabel.text = @"手指上滑,取消发送";
            }
            break;
        default:
            break;
    }
}

#pragma mark 录制结束委托
/**
 *  @Author lpm, 15-10-26 16:10:52
 *
 *  @brief  录制结束
 *
 *  @param recorderUrl
 *  @param audioDuration
 *  @param flag          
 */
-(void)audioRecorderDidFinishRecording:(NSString *)recorderUrl fileName:(NSString *)fileName audioDuration:(float)audioDuration successfully:(BOOL)flag
{
    _isRecordSuc = flag;
    if(_fingerPoint.y < -50)
    {
        _isRecordSuc = NO;
    }
    
    if (audioDuration < recordMinTime)
    {
        _isRecordSuc = NO;
        [[TKIMLayerUtils instance] showTip:@"录音时间太短"];
    }
    
    if (_isRecordSuc)
    {
        TKIMAudioMessage *audio  = [[TKIMAudioMessage alloc] init];
        audio.audioDuration = audioDuration;
        audio.localUrl = recorderUrl;
        audio.type = TKIMMsgTypeAudio;
        audio.isSender = YES;
        audio.fileName = fileName;
        [self routerEventWithName:TKAudioSendEvent userInfo:audio];
    }
    else
    {
        [_audioHelper cancel];
    }
}

/**
 *  @Author lpm, 15-10-26 16:10:43
 *
 *  @brief  音量改变
 *
 *  @param progress
 */
-(void)audioPowerChange:(CGFloat)progress
{
    [self setVoiceImage:progress];
}


/**
 *  @Author lpm, 15-10-26 15:10:41
 *
 *  @brief  显示音量波动视图
 */
-(void)showPowerView:(BOOL) isShow
{
    
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    if(!_audioPowerView)
    {
        _audioPowerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0,window.frame.size.width, window.frame.size.height*0.5)];
        _audioPowerView.backgroundColor = [UIColor clearColor];
        
        UIView *layerView = [[UIView alloc] initWithFrame:CGRectMake((window.frame.size.width-108)/2.0, window.frame.size.height*0.3,108, 100)];
        layerView.backgroundColor = [UIColor blackColor];
        layerView.alpha = 0.5;
        layerView.layer.cornerRadius = 5.0f;
        
        CGFloat width = 50;
        _audioPowerImageView = [[UIImageView alloc] initWithFrame:CGRectMake(layerView.frame.origin.x+(layerView.frame.size.width-width)/2.0, layerView.frame.origin.y+13,width, width)];
        
        [self setVoiceImage:0.0];
        
        _audioPowerLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, _audioPowerImageView.frame.size.height+_audioPowerImageView.frame.origin.y, _audioPowerView.frame.size.width, 30)];
        _audioPowerLabel.font = [UIFont systemFontOfSize:12.0f];
        _audioPowerLabel.text = @"手指上滑,取消发送";
        _audioPowerLabel.textAlignment = NSTextAlignmentCenter;
        _audioPowerLabel.textColor = [UIColor whiteColor];
        _audioPowerLabel.backgroundColor = [UIColor clearColor];
        [_audioPowerView addSubview:layerView];

        [_audioPowerView addSubview:_audioPowerImageView];
        [_audioPowerView addSubview:_audioPowerLabel];
        [window addSubview:_audioPowerView];
    }
    else
    {
        if (isShow)
        {
            if (!_audioPowerView.superview)
            {
                [window addSubview:_audioPowerView];
            }
        }
        else
        {
            if (_audioPowerView.superview)
            {
                [_audioPowerView removeFromSuperview];
            }
        }
    }
}


/**
 *  @Author lpm, 15-10-26 15:10:13
 *
 *  @brief  设置音量图片
 *
 *  @param voiceSound  音量大小
 */
-(void)setVoiceImage:(CGFloat)voiceSound
{
    _audioPowerImageView.image = TKIMGetImage(@"im_record_animate_01");
    if (0 < voiceSound <=0.10)
    {
        [_audioPowerImageView setImage:TKIMGetImage(@"im_record_animate_01")];
    }
    else if (0.10<voiceSound<=0.30)
    {
        [_audioPowerImageView setImage:TKIMGetImage(@"im_record_animate_02")];
    }
    else if (0.30<voiceSound<=0.50)
    {
        [_audioPowerImageView setImage:TKIMGetImage(@"im_record_animate_03")];
    }
    else if (0.50<voiceSound<=0.60)
    {
        [_audioPowerImageView setImage:TKIMGetImage(@"im_record_animate_04")];
    }
    else if (0.60<voiceSound<=0.75)
    {
        [_audioPowerImageView setImage:TKIMGetImage(@"im_record_animate_05")];
    }
    else if (0.75<voiceSound<=0.80)
    {
        [_audioPowerImageView setImage:TKIMGetImage(@"im_record_animate_06")];
    }
    else if (0.80<voiceSound<=0.95)
    {
        [_audioPowerImageView setImage:TKIMGetImage(@"im_record_animate_07")];
    }

}

@end
